
import Element from './Element'


function createElement(type, props, children) {
    return new Element(type, props, children)
}

function render(vDom) {
    const { type, props, children } = vDom;
    const el = document.createElement(type);
    // 遍历属性
    Object.keys(props).forEach(key => {
        setAttrs(el, key, props[key]);
    })
    children.map(item => {
        item = item instanceof Element
            ?
            render(item)
            :
            document.createTextNode(item);
        // 添加到根节点中
        el.appendChild(item);
    })
    return el;
}

function renderDom(element, rootElement) {
    rootElement.appendChild(element);
}

/***
 * 节点挂上属性
 * @method setAttrs
 * @param element 节点
 * @param property 属性
 * @param value 属性后的值
 */
function setAttrs(element, property, value) {
    switch (property) {
        case 'value':
            if (element.tagName === "INPUT" || element.tagName === "TEXTART"){
                node.value = value;
            } else {
                element.setAttribute(property, value);
            }
            break;
        case 'style':
            element.style.cssText = value;
            break;
        default:
            element.setAttribute(property, value);
            break;
    }
}

export {
    createElement,
    render,
    setAttrs,
    renderDom
}


